using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;

namespace Wips.Domain.Models.Authorities.Model
{
    /// <summary>
    /// 回收站，用于记录系统被删除的数据
    /// </summary>
    [Table("AuthorityRecycleBins")]
    [SugarTable("AuthorityRecycleBins")]
    public class RecycleBin : EntityBase, IAggregateRoot
    {
        /// <summary>
        /// 操作人Id
        /// </summary>
        public string UserId { get; set; }

        /// <summary>
        /// 操作人
        /// </summary>
        [Navigate(NavigateType.ManyToOne, nameof(UserId))]
        public User User { get; set; }

        /// <summary>
        /// IP地址
        /// </summary>
        public string IpAddress { get; set; }

        /// <summary>
        /// 浏览器类型
        /// </summary>
        public string UserAgent { get; set; }

        /// <summary>
        /// 实体类名称
        /// </summary>
        public string EntityClassFullName { get; set; }

        /// <summary>
        /// 主体内容
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 是否已恢复
        /// </summary>
        public bool IsRestored { get; set; }

        /// <summary>
        /// 恢复人Id
        /// </summary>
        public string RestoredUserId { get; set; }

        /// <summary>
        /// 恢复人
        /// </summary>
        [Navigate(NavigateType.OneToOne, nameof(RestoredUserId))]
        public User RestoredUser { get; set; }

        public RecycleBin()
        {
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="ipAddress"></param>
        /// <param name="userAgent"></param>
        /// <param name="entityClassFullName"></param>
        /// <param name="body"></param>
        private RecycleBin(string userId, string ipAddress, string userAgent, string entityClassFullName, string body)
        {
            UserId = userId;
            IpAddress = ipAddress;
            UserAgent = userAgent;
            EntityClassFullName = entityClassFullName;
            Body = body;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="ipAddress"></param>
        /// <param name="userAgent"></param>
        /// <param name="entity"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static RecycleBin Create<T>(string userId, string ipAddress, string userAgent, T entity)
        {
            return new RecycleBin(userId, ipAddress, userAgent, typeof(T).FullName, JsonSerializer.Serialize(entity));
        }

        /// <summary>
        /// 恢复数据
        /// </summary>
        /// <param name="restoredUserId"></param>
        public void Restore(string restoredUserId)
        {
            // 如果已经恢复
            if (IsRestored)
            {
                return;
            }

            RestoredUserId = restoredUserId;
            Updated = DateTime.Now;
            IsRestored = true;
            // 添加领域事件
            //AddDomainEvent(new RecycleBinRestoredEvent(Id, EntityClassFullName, Body, restoredUserId));
        }
    }
}